package bill.layuiboot.controller;

import bill.layuiboot.entity.Item;
import bill.layuiboot.service.ProductService;
import bill.layuiboot.vo.DataVO;
import bill.layuiboot.vo.ProductVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Bill Ludwig; 2020/5/31 10:50
 */
@Controller
@RequestMapping("/")
public class TableController {

    @Resource
    private ProductService productService;

    @GetMapping("/list")
    @ResponseBody
    public DataVO<ProductVO> list(Integer page, Integer limit) {
        return productService.findData(page, limit);
    }

    @GetMapping
    public String table() {
        return "table";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        productService.delete(id);
        return "redirect:/";
    }

    @PostMapping("/edit")  // 采用了前后端分离，json数据接受
    public String edit(@RequestBody Item item) {
        var id = item.getId();
        var price = item.getPrice();
        productService.edit(id, price);
        return "redirect:/";
    }
}
